﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Columbus.Tests
{
    [TestClass]
    public class ExampleControllerMakeSynchronousTest
    {
        [TestMethod]
        public void ExampleTestUsingHandMocked()
        {
            // arrange
            var exampleResponse = new ExampleResponse();
            var proxy = new MockWcfServiceClientProxy();
            proxy.SetDoSomethingResult(exampleResponse);

            // act
            var response = Execute.AsyncPattern<ExampleResponse>(proxy.BeginDoSomething, proxy.EndDoSomething);

            // assert
            Assert.AreSame(exampleResponse, response);
        }

        public class MockWcfServiceClientProxy : IWcfServiceClientProxy
        {
            private ExampleResponse doSomethingResponse;

            /// <summary>
            /// Call before Begin method
            /// </summary>
            /// <param name="response"></param>
            public void SetDoSomethingResult(ExampleResponse response)
            {
                doSomethingResponse = response;
            }

            public IAsyncResult BeginDoSomething(AsyncCallback callback, object state)
            {
                //Create a async result, then complete it, setting the completed synchronously flag to true.
                // this will invoke the callback
                var beginDoSomething = new AsyncResult(callback, state);
                beginDoSomething.Complete(doSomethingResponse, true);
                return beginDoSomething;
            }

            public ExampleResponse EndDoSomething(IAsyncResult result)
            {
                return (ExampleResponse)((AsyncResult)result).Result;
            }
        }

        public interface IWcfServiceClientProxy
        {
            IAsyncResult BeginDoSomething(AsyncCallback callback, object state);
            ExampleResponse EndDoSomething(IAsyncResult result);
        }

        public class ExampleResponse
        {
        }
    }
}
